<script setup lang='ts'>
import { ref, watch } from 'vue';
import DateSelect from './base/DateSelect.vue'
const props = withDefaults(defineProps<{
    modelValue?: number,
    withUndefined?: boolean,
    defaultValue?: number,
    readonly?: boolean,
    propertyName?: string
}>(), { withUndefined: true, readonly: false, propertyName: '属性值' })

const numbersRef = ref<any>()

const emits = defineEmits(["update:modelValue"]);
//时间戳转换为时间
const timestampToTime = (timestamp: number) => {
    const date = new Date(timestamp); // 根据时间戳创建Date对象
    const year = date.getFullYear(); // 获取年份
    const month = date.getMonth() + 1; // 获取月份，需要加1
    const day = date.getDate(); // 获取日期
    const hour = date.getHours(); // 获取小时
    const minute = date.getMinutes(); // 获取分钟
    const second = date.getSeconds(); // 获取秒数
    const millisecond = date.getMilliseconds(); // 获取毫秒数
    const formattedDate = `${year}-${month.toString().length === 1 ? `0${month}` : month}-${day.toString().length === 1 ? `0${day}` : day} ${hour.toString().length === 1 ? `0${hour}` : hour}:${minute.toString().length === 1 ? `0${minute}` : minute}:${second.toString().length === 1 ? `0${second}` : second}.${millisecond}`; // 拼接成格式化后的日期字符串
    // const formattedDate = `${year}-${month}-${day}`; // 拼接成格式化后的日期字符串
    // console.log('时间戳转换为时间', formattedDate);
    return formattedDate
}
watch(() => props.modelValue, () => {
    if (!props.defaultValue) return
    numbersRef.value = timestampToTime(props.modelValue ?? props.defaultValue)

}, { immediate: true })
//时间转换为时间戳
const toTimetampTimes = (dateTime: string) => {
    const date = new Date(dateTime)
    const time = date.getTime()
    return time
}
const inputHandler = (e: any) => {
    emits('update:modelValue', toTimetampTimes(e.target.value))

}
</script>

<template>
    <div>
        <input type="datetime-local" name="time" step=".1" :disabled="readonly" v-model="numbersRef"
            @input="inputHandler($event)">
    </div>
</template>

<style scoped>
input {
    width: 190px;
    height: 20px;
    border: 1px solid #3B3C40;
    outline: none;
    background: rgba(28, 28, 29, 0.6);
    color: rgba(230, 230, 230, 1);
    border-radius: 4px;

}
</style>
